<?php
App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');

class CaptchaController extends AppController {

     public $components = array('Acl', 'Link',"Captcha");
	
	public function get_captcha()
	{
	    $this->autoRender = false;
	    App::import('Component','Captcha');
	    //generate random charcters for captcha
	    $random = mt_rand(100, 99999);	
	    //save characters in session
	    $this->Session->write('captcha_code', $random);  
	    $settings = array(
	         'characters' => $random,  
	  'winHeight' => 70,         // captcha image height
	  'winWidth' => 200,	   // captcha image width
	  'fontSize' => 18,          // captcha image characters fontsize
	  'fontPath' => WWW_ROOT.'fonts/Tahoma.ttf',    // captcha image font
	  'noiseColor' => '#ccc',
	  'bgColor' => '#fff',
	  'noiseLevel' => '100',
	  'textColor' => '#000'
	    );
	    $img = $this->Captcha->ShowImage($settings);
	    echo $img;
	}
	public function checkCaptcha(){
		
		if ($this -> request -> is('post')) {
			$captcha = $this -> request ->data['captcha'];
			$captcha_ss = $this->Session->read('captcha_code');
			
			if($captcha != $captcha_ss){
				echo json_encode(array('err'=>'2', 'msg'=>$captcha."-Mã xác nhận ko đúng=".$captcha_ss));
				exit;
			}else{
				echo json_encode(array('err'=>'1', 'msg'=>"ok"));
				exit;
			}
		}
		echo json_encode(array('err'=>'2', 'msg'=>"Không hợp lệ"));
		exit;
	}
	
	

}
?>